package cn.source.system.service.impl;

import java.text.DecimalFormat;
import java.util.List;

import cn.hutool.core.util.IdUtil;
import cn.source.common.core.redis.RedisCache;
import cn.source.common.utils.DateUtils;
import cn.source.school.service.ISchoolService;
import cn.source.system.domain.*;
import cn.source.system.mapper.GkZytbDetailMapper;
import cn.source.system.service.IBuzMemberGradeService;
import cn.source.system.service.IBuzMemberService;
import cn.source.system.service.ISchoolPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import cn.source.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import cn.source.system.mapper.GkZytbMapper;
import cn.source.system.service.IGkZytbService;

/**
 * 志愿填报主Service业务层处理
 * 
 * @author sourcebyte.vip
 * @date 2024-12-02
 */
@Service
public class GkZytbServiceImpl implements IGkZytbService 
{
    @Autowired
    private GkZytbMapper gkZytbMapper;

    @Autowired
    private GkZytbDetailMapper gkZytbDetailMapper;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private IBuzMemberService buzMemberService;

    @Autowired
    IBuzMemberGradeService buzMemberGradeService;

    @Autowired
    private ISchoolPlanService schoolPlanService;

    @Autowired
    private ISchoolService schoolService;

    //今年招生计划、今年的分数线
    private Integer thisYear=2024;

    /*近3年本科录取分数线*/
    private int gradeLineLg2022=347;
    private int gradeLineLg2021=343;
    private int gradeLineLg2020=348;

    private int gradeLineWs2022=428;
    private int gradeLineWs2021=421;
    private int gradeLineWs2020=413;

    //近3年
    private int yearNew1=2023;
    private int yearNew2=2022;
    private int yearNew3=2021;

    /**
     * 查询志愿填报主
     * 
     * @param id 志愿填报主主键
     * @return 志愿填报主
     */
    @Override
    public GkZytb selectGkZytbById(String id)
    {
        return gkZytbMapper.selectGkZytbById(id);
    }

    /**
     * 查询志愿填报主列表
     * 
     * @param gkZytb 志愿填报主
     * @return 志愿填报主
     */
    @Override
    public List<GkZytb> selectGkZytbList(GkZytb gkZytb)
    {
        return gkZytbMapper.selectGkZytbList(gkZytb);
    }

    public List<GkZytb> selectGkZytbMaxZyseqList(GkZytb gkZytb)
    {
        return gkZytbMapper.selectGkZytbMaxZyseqList(gkZytb);
    }

    /**
     * 新增志愿填报主
     * 
     * @param gkZytb 志愿填报主
     * @return 结果
     */
    @Transactional
    @Override
    public int insertGkZytb(GkZytb gkZytb)
    {
        gkZytb.setCreateTime(DateUtils.getNowDate());
        int rows = gkZytbMapper.insertGkZytb(gkZytb);
        insertGkZytbDetail(gkZytb);
        return rows;
    }

    public int insertGkZytbList(List<GkZytb> list){

        for(GkZytb gkZytb : list){
            gkZytb.setCreateTime(DateUtils.getNowDate());
            gkZytb.setCreateBy(gkZytb.getMemberId());
        }

        return gkZytbMapper.insertGkZytbList(list);
    }

    @Async
    public Integer insertGkZytbDetailByPcId(String  pcId){
        System.out.println("------------pcId-----------"+pcId);

        //根据批次查找所有的志愿
        Object pcObj = redisCache.getCacheObject(pcId);
        if(pcObj!=null) {

            String userId = "";

            List<GkZytb> allList=(List<GkZytb>)pcObj;
            for(int i=0;i<allList.size();i++){
                GkZytb tb=allList.get(i);
                userId=tb.getMemberId();

                break;
            }

            BuzMember memberQuery=buzMemberService.selectBuzMemberById(userId);

            //查找会员的同位分
            BuzMemberGrade memberGradeQuery=new BuzMemberGrade();
            memberGradeQuery.setMemberId(memberQuery.getId());
            List<BuzMemberGrade> memberGradeList=buzMemberGradeService.selectBuzMemberGradeList(memberGradeQuery);



            DecimalFormat df = new DecimalFormat("#.00");
            //开始循环学校、专业组，生成专业明细
            for(int m=0;m<allList.size();m++) {
                GkZytb tb = allList.get(m);

                List<GkZytbDetail> gkZytbDetailList=new ArrayList();

                String province=tb.getProvince();
                String scode=tb.getSchoolCode();
                String zyz= tb.getZyzCode();

                SchoolPlan schoolPlanQuery=new SchoolPlan();
                schoolPlanQuery.setYear(thisYear);
                schoolPlanQuery.setSchoolCode(scode);
                schoolPlanQuery.setZyz(zyz);
                if("物理".equals(memberQuery.getSxkm())) {
                    schoolPlanQuery.setMajorType("理工类");
                }
                if("历史".equals(memberQuery.getSxkm())) {
                    schoolPlanQuery.setMajorType("文史类");
                }

                //如果会员不是少数民族，则不能查询民族班
                if(memberQuery.getSfssmz()!=null&&(memberQuery.getSfssmz()).intValue()==0) {
                    schoolPlanQuery.setSsmzFlag(0);
                }

                if(memberQuery.getSfgjzxdfzx()!=null&&(memberQuery.getSfgjzxdfzx()).intValue()==0) {
                    schoolPlanQuery.setGjzxFlag(0);
                }

                if(memberQuery.getSfdfzx()!=null&&(memberQuery.getSfdfzx()).intValue()==0) {
                    schoolPlanQuery.setGxzxFlag(0);
                }

                //考生的再选科目
                schoolPlanQuery.setZxkm(memberQuery.getZxkm());

                //根据学校、专业组查询 所有的专业
                List<SchoolPlan> zyzList =schoolPlanService.selectSchoolMajorPlanList(schoolPlanQuery);

                //查找近3年该学校、专业组下各专业的录取数据
                //需要查找每个专业近3年的数据
                String zyCode="";//列表中所有的专业编码
                if(zyzList!=null&&zyzList.size()>0) {
                    for(int i=0;i<zyzList.size();i++) {
                        SchoolPlan school=zyzList.get(i);
                        zyCode=zyCode+"'"+school.getMajorCode()+"',";
                    }
                    zyCode=zyCode.substring(0,zyCode.length()-1);
                }
                schoolPlanQuery.setYear3(yearNew1+","+yearNew2+","+yearNew3);
                schoolPlanQuery.setMajorName(zyCode);
                List<SchoolPlan> majorList =schoolPlanService.selectSchoolMajorList3Year(schoolPlanQuery);


                schoolPlanQuery=new SchoolPlan();
                schoolPlanQuery.setYear(thisYear);
                schoolPlanQuery.setSchoolCode(scode);
                if("物理".equals(memberQuery.getSxkm())) {
                    schoolPlanQuery.setMajorType("理工类");
                }
                if("历史".equals(memberQuery.getSxkm())) {
                    schoolPlanQuery.setMajorType("文史类");
                }

                schoolPlanQuery.setZyz(zyz);
                List<SchoolPlan> plans2024List =schoolPlanService.selectSchoolZyz2024PlanList(schoolPlanQuery);


                //考生近3年的广西排位
                int gxj2022=0;
                int gxj2021=0;
                int gxj2020=0;
                //考生近3年全国排位分
                int qgj2022=0;
                int qgj2021=0;
                int qgj2020=0;
                /**
                 * 计算当年的排名和对应的分数
                 */
                if(province.equals("450000")) {
                    if (memberGradeList != null && memberGradeList.size() > 0) {
                        for (int k1 = 0; k1 < memberGradeList.size(); k1++) {
                            BuzMemberGrade memberGrade = memberGradeList.get(k1);
                            if (memberGrade.getYear().intValue() == yearNew1) {
                                if (memberGrade.getPmGradeGx() != null) {
                                    gxj2022=memberGrade.getPmGradeGx();
                                }
                            }
                            if (memberGrade.getYear().intValue() == yearNew2) {
                                if (memberGrade.getPmGradeGx() != null) {
                                    gxj2021=memberGrade.getPmGradeGx();
                                }
                            }
                            if (memberGrade.getYear().intValue() == yearNew3) {
                                if (memberGrade.getPmGradeGx() != null) {
                                    gxj2020=memberGrade.getPmGradeGx();
                                }
                            }
                        }
                    }
                } else {
                    if (memberGradeList != null && memberGradeList.size() > 0) {
                        for (int k1 = 0; k1 < memberGradeList.size(); k1++) {
                            BuzMemberGrade memberGrade = memberGradeList.get(k1);
                            if (memberGrade.getYear().intValue() == yearNew1) {
                                if (memberGrade.getPmGradeGx() != null) {
                                    qgj2022=memberGrade.getPmGrade();
                                }
                            }
                            if (memberGrade.getYear().intValue() == yearNew2) {
                                if (memberGrade.getPmGradeGx() != null) {
                                    qgj2021=memberGrade.getPmGrade();
                                }
                            }
                            if (memberGrade.getYear().intValue() == yearNew3) {
                                if (memberGrade.getPmGradeGx() != null) {
                                    qgj2020=memberGrade.getPmGrade();
                                }
                            }
                        }
                    }
                }


                for(int i=0;i< zyzList.size();i++) {
                    SchoolPlan zyzPlan=zyzList.get(i);

                    Integer majorPlan=zyzPlan.getMajorPlan();
                    if(majorPlan==null) majorPlan=0;



                    GkZytbDetail gkZytbDetail=new GkZytbDetail();
                    gkZytbDetail.setId(IdUtil.getSnowflakeNextIdStr());
                    gkZytbDetail.setGkZytbId(tb.getId());
                    gkZytbDetail.setUserId(userId);
                    gkZytbDetail.setMajorCode(zyzPlan.getMajorCode());
                    gkZytbDetail.setMajorName(zyzPlan.getMajorName());
                    gkZytbDetail.setMajorPm(zyzPlan.getMajorPm());
                    gkZytbDetail.setPlanNums(zyzPlan.getMajorPlan());





                    int zyavgGrade2022=0;
                    int zyminGrade2022=0;
                    int zyavgGrade2021=0;
                    int zyminGrade2021=0;
                    int zyavgGrade2020=0;
                    int zyminGrade2020=0;


                    double zySubmitRate=0;
                    double finalZySubmitRate=0;

                    //近3年的专业数据
                    for(int k=0;k<majorList.size();k++) {
                        SchoolPlan schoolPlan = majorList.get(k);
                        if (zyzPlan.getSchoolCode().equals(schoolPlan.getSchoolCode()) &&
                                schoolPlan.getZyz().equals(zyzPlan.getZyz()) &&
                                zyzPlan.getMajorCode().equals(schoolPlan.getMajorCode())) {

                            if (schoolPlan.getYear().intValue() == yearNew1) {
                                if (schoolPlan.getMajorAvgGrade() != null) {
                                    zyavgGrade2022 = schoolPlan.getMajorAvgGrade();
                                }
                                if (schoolPlan.getMajorLowGrade() != null) {
                                    zyminGrade2022 = schoolPlan.getMajorLowGrade();
                                }
                            }

                            if (schoolPlan.getYear().intValue() == yearNew2) {
                                if (schoolPlan.getMajorAvgGrade() != null) {
                                    zyavgGrade2021 = schoolPlan.getMajorAvgGrade();
                                }
                                if (schoolPlan.getMajorLowGrade() != null) {
                                    zyminGrade2021 = schoolPlan.getMajorLowGrade();
                                }
                            }

                            if (schoolPlan.getYear().intValue() == yearNew3) {
                                if (schoolPlan.getMajorAvgGrade() != null) {
                                    zyavgGrade2020 = schoolPlan.getMajorAvgGrade();
                                }
                                if (schoolPlan.getMajorLowGrade() != null) {
                                    zyminGrade2020 = schoolPlan.getMajorLowGrade();
                                }
                            }


                            if(province.equals("450000")) {
                                int memberGxGrade=memberQuery.getGxgrade();
                                int yearLowGrade=zyminGrade2022;
                                if(yearLowGrade==0)	yearLowGrade=zyminGrade2021;
                                if(yearLowGrade==0)	yearLowGrade=zyminGrade2020;
                                if(memberGxGrade==yearLowGrade){
                                    zySubmitRate=50;

                                }else{
                                    float cha=(float)(memberGxGrade-yearLowGrade)/2;
                                    if(cha<0){
                                        cha=cha-1+50;
                                    }else{
                                        cha=cha+1+50;
                                    }
                                    if(cha>=100) cha=99.99f;
                                    if(cha<1) cha=1;
                                    zySubmitRate=cha;

                                }
                            }else{
                                int memberGxGrade=memberQuery.getQggrade();
                                int yearLowGrade=zyminGrade2022;
                                if(yearLowGrade==0)	yearLowGrade=zyminGrade2021;
                                if(yearLowGrade==0)	yearLowGrade=zyminGrade2020;
                                if(memberGxGrade==yearLowGrade){
                                    zySubmitRate=50;
                                }else{
                                    float cha=(float)(memberGxGrade-yearLowGrade)/2;
                                    if(cha<0){
                                        cha=cha-1+50;
                                    }else{
                                        cha=cha+1+50;
                                    }
                                    if(cha>=100) cha=99.99f;
                                    if(cha<1) cha=1;
                                    zySubmitRate=cha;
                                }
                            }
                            gkZytbDetail.setRateSubmit(df.format(zySubmitRate));

                            /*
                            if (zyavgGrade2020 > 0 && zyavgGrade2021 > 0 && zyavgGrade2022 > 0) {//3年都有数据
                                if(province.equals("450000")) {
                                    if (gxj2020 - zyavgGrade2020 > 0 && gxj2021 - zyavgGrade2021 > 0 && gxj2022 - zyavgGrade2022 > 0) {//3年的同位分都比录取平均分高
                                        double rate = ((gxj2022 - zyavgGrade2022) / 2 + 80) * 0.55 + ((gxj2021 - zyavgGrade2021) / 2 + 80) * 0.3 + ((gxj2020 - zyavgGrade2020) / 2 + 80) * 0.15;


                                        if (rate >= 100) rate = 99.99;
                                        if (rate < 1) rate = 1;
                                        zySubmitRate = rate;
                                    } else {
                                        double rate = 0;
                                        //2年高于
                                        int count = 0;
                                        if (gxj2022 - zyavgGrade2022 > 0) {
                                            count++;
                                        }
                                        if (gxj2021 - zyavgGrade2021 > 0) {
                                            count++;
                                        }
                                        if (gxj2020 - zyavgGrade2020 > 0) {
                                            count++;
                                        }
                                        if (count == 2) {//有2年高于同位分的

                                            if (gxj2022 - zyavgGrade2022 > 0) {
                                                rate = rate + ((gxj2022 - zyavgGrade2022) / 2 + 70) * 0.55;
                                            } else {
                                                rate = rate + ((gxj2022 - zyminGrade2022) / 2 + 50) * 0.55;
                                            }

                                            if (gxj2021 - zyavgGrade2021 > 0) {
                                                rate = rate + ((gxj2021 - zyavgGrade2021) / 2 + 70) * 0.3;
                                            } else {
                                                rate = rate + ((gxj2021 - zyminGrade2021) / 2 + 50) * 0.3;
                                            }

                                            if (gxj2020 - zyavgGrade2020 > 0) {
                                                rate = rate + ((gxj2020 - zyavgGrade2020) / 2 + 70) * 0.15;
                                            } else {
                                                rate = rate + ((gxj2020 - zyminGrade2020) / 2 + 50) * 0.15;
                                            }
                                        } else if (count == 1) {//有1年高于同位分的

                                            if (gxj2022 - zyavgGrade2022 > 0) {
                                                rate = rate + ((gxj2022 - zyavgGrade2022) / 2 + 60) * 0.55;
                                            } else {
                                                rate = rate + ((gxj2022 - zyminGrade2022) / 2 + 50) * 0.55;
                                            }

                                            if (gxj2021 - zyavgGrade2021 > 0) {
                                                rate = rate + ((gxj2021 - zyavgGrade2021) / 2 + 60) * 0.3;
                                            } else {
                                                rate = rate + ((gxj2021 - zyminGrade2021) / 2 + 50) * 0.3;
                                            }

                                            if (gxj2020 - zyavgGrade2020 > 0) {
                                                rate = rate + ((gxj2020 - zyavgGrade2020) / 2 + 60) * 0.15;
                                            } else {
                                                rate = rate + ((gxj2020 - zyminGrade2020) / 2 + 50) * 0.15;
                                            }

                                        } else {//一个高于的都没有
                                            rate = ((gxj2022 - zyminGrade2022) / 2 + 50) * 0.55 + ((gxj2021 - zyminGrade2021) / 2 + 50) * 0.3 + ((gxj2020 - zyminGrade2020) / 2 + 50) * 0.15;
                                        }


                                        if (rate >= 100) rate = 99.99;
                                        if (rate < 1) rate = 1;
                                        zySubmitRate = rate;
                                    }
                                } else {//全国学校，用全国同位分
                                    if (qgj2020 - zyavgGrade2020 > 0 && qgj2021 - zyavgGrade2021 > 0 && qgj2022 - zyavgGrade2022 > 0) {//3年的同位分都比录取平均分高
                                        double rate = ((qgj2022 - zyavgGrade2022) / 2 + 80) * 0.55 + ((qgj2021 - zyavgGrade2021) / 2 + 80) * 0.3 + ((qgj2020 - zyavgGrade2020) / 2 + 80) * 0.15;

                                        if (rate >= 100) rate = 99.99;
                                        if (rate < 1) rate = 1;
                                        zySubmitRate = rate;
                                    } else {
                                        double rate = 0;
                                        //2年高于
                                        int count = 0;
                                        if (qgj2022 - zyavgGrade2022 > 0) {
                                            count++;
                                        }
                                        if (qgj2021 - zyavgGrade2021 > 0) {
                                            count++;
                                        }
                                        if (qgj2020 - zyavgGrade2020 > 0) {
                                            count++;
                                        }
                                        if (count == 2) {//有2年高于同位分的

                                            if (qgj2022 - zyavgGrade2022 > 0) {
                                                rate = rate + ((qgj2022 - zyavgGrade2022) / 2 + 70) * 0.55;
                                            } else {
                                                rate = rate + ((qgj2022 - zyminGrade2022) / 2 + 50) * 0.55;
                                            }

                                            if (qgj2021 - zyavgGrade2021 > 0) {
                                                rate = rate + ((qgj2021 - zyavgGrade2021) / 2 + 70) * 0.3;
                                            } else {
                                                rate = rate + ((qgj2021 - zyminGrade2021) / 2 + 50) * 0.3;
                                            }

                                            if (qgj2020 - zyavgGrade2020 > 0) {
                                                rate = rate + ((qgj2020 - zyavgGrade2020) / 2 + 70) * 0.15;
                                            } else {
                                                rate = rate + ((qgj2020 - zyminGrade2020) / 2 + 50) * 0.15;
                                            }
                                        } else if (count == 1) {//有1年高于同位分的

                                            if (qgj2022 - zyavgGrade2022 > 0) {
                                                rate = rate + ((qgj2022 - zyavgGrade2022) / 2 + 60) * 0.55;
                                            } else {
                                                rate = rate + ((qgj2022 - zyminGrade2022) / 2 + 50) * 0.55;
                                            }

                                            if (qgj2021 - zyavgGrade2021 > 0) {
                                                rate = rate + ((qgj2021 - zyavgGrade2021) / 2 + 60) * 0.3;
                                            } else {
                                                rate = rate + ((qgj2021 - zyminGrade2021) / 2 + 50) * 0.3;
                                            }

                                            if (qgj2020 - zyavgGrade2020 > 0) {
                                                rate = rate + ((qgj2020 - zyavgGrade2020) / 2 + 60) * 0.15;
                                            } else {
                                                rate = rate + ((qgj2020 - zyminGrade2020) / 2 + 50) * 0.15;
                                            }

                                        } else {//一个高于的都没有
                                            rate = ((qgj2022 - zyminGrade2022) / 2 + 50) * 0.55 + ((qgj2021 - zyminGrade2021) / 2 + 50) * 0.3 + ((qgj2020 - zyminGrade2020) / 2 + 50) * 0.15;
                                        }


                                        if (rate >= 100) rate = 99.99;
                                        if (rate < 1) rate = 1;
                                        zySubmitRate = rate;
                                    }
                                }
                            } else {//数据不满3年
                                int years = 0;
                                if (zyavgGrade2020 > 0) years++;
                                if (zyavgGrade2021 > 0) years++;
                                if (zyavgGrade2022 > 0) years++;
                                if (years == 2) {
                                    double rate = 0;
                                    if(province.equals("450000")) {
                                        int heightYears = 0;//这两年中，同位分大于平均分的年份数=2，则+70，如果年份数=1，则+60，都不大于平均分，则+50
                                        if (zyavgGrade2022 > 0 && gxj2022 - zyavgGrade2022 > 0) heightYears++;
                                        if (zyavgGrade2021 > 0 && gxj2021 - zyavgGrade2021 > 0) heightYears++;
                                        if (zyavgGrade2020 > 0 && gxj2020 - zyavgGrade2020 > 0) heightYears++;

                                        if (zyavgGrade2022 > 0) {//2022年有数据
                                            if (heightYears == 2) {
                                                double rate1 = ((gxj2022 - zyavgGrade2022) / 2 + 70) * 0.60;
                                                rate = rate + rate1;

                                                if (zyavgGrade2021 > 0) {
                                                    double rate2 = ((gxj2021 - zyavgGrade2021) / 2 + 70) * 0.40;
                                                    rate = rate + rate2;
                                                } else {
                                                    double rate2 = ((gxj2020 - zyavgGrade2020) / 2 + 70) * 0.40;
                                                    rate = rate + rate2;
                                                }
                                            } else if (heightYears == 1) {
                                                if (gxj2022 - zyavgGrade2022 > 0) {
                                                    double rate1 = ((gxj2022 - zyavgGrade2022) / 2 + 60) * 0.60;
                                                    rate = rate + rate1;
                                                } else {
                                                    double rate1 = ((gxj2022 - zyminGrade2022) / 2 + 50) * 0.60;
                                                    rate = rate + rate1;
                                                }
                                                if (zyavgGrade2021 > 0) {
                                                    if (gxj2021 - zyavgGrade2021 > 0) {
                                                        double rate1 = ((gxj2021 - zyavgGrade2021) / 2 + 60) * 0.40;
                                                        rate = rate + rate1;
                                                    } else {
                                                        double rate1 = ((gxj2021 - zyminGrade2021) / 2 + 60) * 0.40;
                                                        rate = rate + rate1;
                                                    }
                                                } else {
                                                    if (gxj2020 - zyavgGrade2020 > 0) {
                                                        double rate1 = ((gxj2020 - zyavgGrade2020) / 2 + 60) * 0.40;
                                                        rate = rate + rate1;
                                                    } else {
                                                        double rate1 = ((gxj2020 - zyminGrade2020) / 2 + 60) * 0.40;
                                                        rate = rate + rate1;
                                                    }
                                                }
                                            } else {//都不高于平均分
                                                double rate1 = ((gxj2022 - zyminGrade2022) / 2 + 50) * 0.60;
                                                rate = rate + rate1;
                                                if (zyavgGrade2021 > 0) {
                                                    double rate2 = ((gxj2021 - zyminGrade2021) / 2 + 50) * 0.60;
                                                    rate = rate + rate2;
                                                } else {
                                                    double rate2 = ((gxj2020 - zyminGrade2020) / 2 + 50) * 0.40;
                                                    rate = rate + rate2;
                                                }
                                            }
                                        } else {//2022年没数据，只有2021和2020年数据
                                            if (heightYears == 2) {
                                                double rate1 = ((gxj2021 - zyavgGrade2021) / 2 + 70) * 0.60 + ((gxj2020 - zyavgGrade2020) / 2 + 70) * 0.40;
                                                rate = rate + rate1;
                                            } else if (heightYears == 1) {
                                                if (gxj2021 - zyavgGrade2021 > 0) {
                                                    double rate1 = ((gxj2021 - zyavgGrade2021) / 2 + 60) * 0.60;
                                                    rate = rate + rate1;
                                                } else {
                                                    double rate1 = ((gxj2021 - zyminGrade2021) / 2 + 50) * 0.60;
                                                    rate = rate + rate1;
                                                }
                                                if (gxj2020 - zyavgGrade2020 > 0) {
                                                    double rate1 = ((gxj2020 - zyavgGrade2020) / 2 + 60) * 0.40;
                                                    rate = rate + rate1;
                                                } else {
                                                    double rate1 = ((gxj2020 - zyminGrade2020) / 2 + 50) * 0.40;
                                                    rate = rate + rate1;
                                                }
                                            } else {
                                                double rate1 = ((gxj2021 - zyminGrade2021) / 2 + 50) * 0.60;
                                                rate = rate + rate1;

                                                double rate2 = ((gxj2020 - zyminGrade2020) / 2 + 50) * 0.40;
                                                rate = rate + rate2;
                                            }
                                        }
                                    } else {//只有2年数据，全国的
                                        int heightYears = 0;//这两年中，同位分大于平均分的年份数=2，则+70，如果年份数=1，则+60，都不大于平均分，则+50
                                        if (zyavgGrade2022 > 0 && qgj2022 - zyavgGrade2022 > 0) heightYears++;
                                        if (zyavgGrade2021 > 0 && qgj2021 - zyavgGrade2021 > 0) heightYears++;
                                        if (zyavgGrade2020 > 0 && qgj2020 - zyavgGrade2020 > 0) heightYears++;

                                        if (zyavgGrade2022 > 0) {//2022年有数据
                                            if (heightYears == 2) {
                                                double rate1 = ((qgj2022 - zyavgGrade2022) / 2 + 70) * 0.60;
                                                rate = rate + rate1;

                                                if (zyavgGrade2021 > 0) {
                                                    double rate2 = ((qgj2021 - zyavgGrade2021) / 2 + 70) * 0.40;
                                                    rate = rate + rate2;
                                                } else {
                                                    double rate2 = ((qgj2020 - zyavgGrade2020) / 2 + 70) * 0.40;
                                                    rate = rate + rate2;
                                                }
                                            } else if (heightYears == 1) {
                                                if (qgj2022 - zyavgGrade2022 > 0) {
                                                    double rate1 = ((qgj2022 - zyavgGrade2022) / 2 + 60) * 0.60;
                                                    rate = rate + rate1;
                                                } else {
                                                    double rate1 = ((qgj2022 - zyminGrade2022) / 2 + 50) * 0.60;
                                                    rate = rate + rate1;
                                                }
                                                if (zyavgGrade2021 > 0) {
                                                    if (qgj2021 - zyavgGrade2021 > 0) {
                                                        double rate1 = ((qgj2021 - zyavgGrade2021) / 2 + 60) * 0.40;
                                                        rate = rate + rate1;
                                                    } else {
                                                        double rate1 = ((qgj2021 - zyminGrade2021) / 2 + 60) * 0.40;
                                                        rate = rate + rate1;
                                                    }
                                                } else {
                                                    if (qgj2020 - zyavgGrade2020 > 0) {
                                                        double rate1 = ((qgj2020 - zyavgGrade2020) / 2 + 60) * 0.40;
                                                        rate = rate + rate1;
                                                    } else {
                                                        double rate1 = ((qgj2020 - zyminGrade2020) / 2 + 60) * 0.40;
                                                        rate = rate + rate1;
                                                    }
                                                }
                                            } else {//都不高于平均分
                                                double rate1 = ((qgj2022 - zyminGrade2022) / 2 + 50) * 0.60;
                                                rate = rate + rate1;
                                                if (zyavgGrade2021 > 0) {
                                                    double rate2 = ((qgj2021 - zyminGrade2021) / 2 + 50) * 0.60;
                                                    rate = rate + rate2;
                                                } else {
                                                    double rate2 = ((qgj2020 - zyminGrade2020) / 2 + 50) * 0.40;
                                                    rate = rate + rate2;
                                                }
                                            }
                                        } else {//2022年没数据，只有2021和2020年数据
                                            if (heightYears == 2) {
                                                double rate1 = ((qgj2021 - zyavgGrade2021) / 2 + 70) * 0.60 + ((qgj2020 - zyavgGrade2020) / 2 + 70) * 0.40;
                                                rate = rate + rate1;
                                            } else if (heightYears == 1) {
                                                if (qgj2021 - zyavgGrade2021 > 0) {
                                                    double rate1 = ((qgj2021 - zyavgGrade2021) / 2 + 60) * 0.60;
                                                    rate = rate + rate1;
                                                } else {
                                                    double rate1 = ((qgj2021 - zyminGrade2021) / 2 + 50) * 0.60;
                                                    rate = rate + rate1;
                                                }
                                                if (qgj2020 - zyavgGrade2020 > 0) {
                                                    double rate1 = ((qgj2020 - zyavgGrade2020) / 2 + 60) * 0.40;
                                                    rate = rate + rate1;
                                                } else {
                                                    double rate1 = ((qgj2020 - zyminGrade2020) / 2 + 50) * 0.40;
                                                    rate = rate + rate1;
                                                }
                                            } else {
                                                double rate1 = ((qgj2021 - zyminGrade2021) / 2 + 50) * 0.60;
                                                rate = rate + rate1;

                                                double rate2 = ((qgj2020 - zyminGrade2020) / 2 + 50) * 0.40;
                                                rate = rate + rate2;
                                            }
                                        }

                                    }


                                    if (rate >= 100) rate = 99.99;
                                    if (rate < 1) rate = 1;
                                    zySubmitRate = rate;
                                } else {//只有一年数据
                                    if(province.equals("450000")) {

                                        if (zyavgGrade2022 > 0) {
                                            double rate = ((gxj2022 - zyminGrade2022) / 2) + 50;

                                            if (rate >= 100) rate = 99.99;
                                            if (rate < 1) rate = 1;
                                            zySubmitRate = rate;
                                        }
                                        if (zyavgGrade2021 > 0) {
                                            double rate = ((gxj2021 - zyminGrade2021) / 2) + 50;

                                            if (rate >= 100) rate = 99.99;
                                            if (rate < 1) rate = 1;
                                            zySubmitRate = rate;
                                        }
                                        if (zyavgGrade2020 > 0) {
                                            double rate = ((gxj2020 - zyminGrade2020) / 2) + 50;

                                            if (rate >= 100) rate = 99.99;
                                            if (rate < 1) rate = 1;
                                            zySubmitRate = rate;
                                        }
                                    } else {//全国只有一年数据
                                        if (zyavgGrade2022 > 0) {
                                            double rate = ((qgj2022 - zyminGrade2022) / 2) + 50;

                                            if (rate >= 100) rate = 99.99;
                                            if (rate < 1) rate = 1;
                                            zySubmitRate = rate;
                                        }
                                        if (zyavgGrade2021 > 0) {
                                            double rate = ((qgj2021 - zyminGrade2021) / 2) + 50;

                                            if (rate >= 100) rate = 99.99;
                                            if (rate < 1) rate = 1;
                                            zySubmitRate = rate;
                                        }
                                        if (zyavgGrade2020 > 0) {
                                            double rate = ((qgj2020 - zyminGrade2020) / 2) + 50;

                                            if (rate >= 100) rate = 99.99;
                                            if (rate < 1) rate = 1;
                                            zySubmitRate = rate;
                                        }
                                    }
                                }
                            }

                             */
                        }
                    }



			/*
					专业的录取概率:(今年的院校专业组投档概率X80%+现在系统显示的录取概率Ⅹ20%）
					 */
                    /*
                    for(int s=0;s<plans2024List.size();s++){
                        SchoolPlan plan2024= plans2024List.get(s);
                        //学校和专业组相等
                        if(plan2024.getSchoolCode().equals(scode)
                                &&plan2024.getZyz().equals(zyz)){
                            //最低录取分数线
                            int grade=plan2024.getGrade();
                            if(province!=null){
                                if(province.equals("450000")) {
                                    int memberGrade=memberQuery.getGxgrade();
                                    //((考生成绩-录取最低分)/2+50)*0.8+schoolRecord2022.getSubmitRate()*0.2
                                    finalZySubmitRate = ((memberGrade - grade) / 2 + 50) * 0.8 + zySubmitRate * 0.2;
                                    if(finalZySubmitRate>=100) finalZySubmitRate=99.99;
                                    if(finalZySubmitRate<1) finalZySubmitRate=1;
                                }else {
                                    int memberGrade=memberQuery.getQggrade();
                                    //((考生成绩-录取最低分)/2+50)*0.8+schoolRecord2022.getSubmitRate()*0.2
                                    finalZySubmitRate = ((memberGrade - grade) / 2 + 50) * 0.8 + zySubmitRate * 0.2;
                                    if(finalZySubmitRate>=100) finalZySubmitRate=99.99;
                                    if(finalZySubmitRate<1) finalZySubmitRate=1;
                                }
                                zyzPlan.setZySubmitRate(df.format(finalZySubmitRate));//专业投档概率

                                gkZytbDetail.setRateSubmit(df.format(finalZySubmitRate));
                            }else {
                                zyzPlan.setZySubmitRate("-");//专业投档概率
                            }
                        }
                    }

                     */


                    gkZytbDetailList.add(gkZytbDetail);

                }//专业循环

                if(gkZytbDetailList.size()>0){
                    gkZytbDetailMapper.insertGkZytbDetailList(gkZytbDetailList);
                }

            }



        }

        return 1;
    }

    /**
     * 修改志愿填报主
     * 
     * @param gkZytb 志愿填报主
     * @return 结果
     */
    @Transactional
    @Override
    public int updateGkZytb(GkZytb gkZytb)
    {
        gkZytb.setUpdateTime(DateUtils.getNowDate());
        gkZytbMapper.deleteGkZytbDetailByGkZytbId(gkZytb.getId());
        insertGkZytbDetail(gkZytb);
        return gkZytbMapper.updateGkZytb(gkZytb);
    }

    /**
     * 批量删除志愿填报主
     * 
     * @param ids 需要删除的志愿填报主主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteGkZytbByIds(String[] ids)
    {
        gkZytbMapper.deleteGkZytbDetailByGkZytbIds(ids);
        return gkZytbMapper.deleteGkZytbByIds(ids);
    }

    /**
     * 删除志愿填报主信息
     * 
     * @param id 志愿填报主主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteGkZytbById(String id)
    {
        gkZytbMapper.deleteGkZytbDetailByGkZytbId(id);
        return gkZytbMapper.deleteGkZytbById(id);
    }

    /**
     * 新增模拟志愿填报明细信息
     * 
     * @param gkZytb 志愿填报主对象
     */
    public void insertGkZytbDetail(GkZytb gkZytb)
    {
        List<GkZytbDetail> gkZytbDetailList = gkZytb.getGkZytbDetailList();
        String id = gkZytb.getId();
        if (StringUtils.isNotNull(gkZytbDetailList))
        {
            List<GkZytbDetail> list = new ArrayList<GkZytbDetail>();
            for (GkZytbDetail gkZytbDetail : gkZytbDetailList)
            {
                gkZytbDetail.setGkZytbId(id);
                list.add(gkZytbDetail);
            }
            if (list.size() > 0)
            {
                gkZytbMapper.batchGkZytbDetail(list);
            }
        }
    }
}
